<?php

/**
 * Classe responsavel por todas as comunidades do sistema.
 * @author Rodrigo
 * @package app.Controller
 */
Class CommunitiesController extends AppController{

	public function index(){
		$this->layout = 'base';
		$idUser = $this-> Auth->user('id');
		//comunidades que o usuário não participa			
		$this->set('communities', $this -> Community -> communities($idUser));
		//comunidades do usuário
		$this -> set ('userCommunities', $this -> Community -> userCommunities($idUser)); 
	}
	
    public function add() {
	   	$this->layout ='feed';
		$this->set('idUser',$this->Auth->user('id'));
		//lista de tags
		$this-> set ('tags', $this->Community->Tag->tags());	
		if($this->request->is('post')){
		//Criação de url
		$this->request->data['Community']['url'] = $this->StrToUrl($this->request->data['Community']['name']);
		$this->request->data['Community']['cmmname'] = $this->StrToUrl($this->request->data['Community']['name']);
		//Slavar foto
		$this->request->data['Community']['photo'] =  $this->SaveImg( $this->request->data['Community']['photo'],'communities');
		
			if($this->Community->saveAll($this->request->data)){
				$idCmm = $this->Community->findById($this->Community->id);
				$this-> joinModerador($idCmm['Community']['id'],$this->request->data['Community']['url']);	
			}
			else{
				$this->Session->setFlash(__('Erro!'));
			}
		}
	}	
	
	public function join($idCmm) {
		$idUser = $this->Auth->user('id');
		//insere o usuario como membro da comunidade
		$cmm = $this-> Community -> findById($idCmm);
		if ($cmm['Community']['private'] == 0){
		$msg = 'Você agora participa dessa comunidade';
		$this->Community->joinAprovado($idUser, $idCmm);
		}else{
		$msg = 'Solicitação de participação enviada!';
		$this->Community->joinPendente($idUser, $idCmm);
		}
		$this->Session->setFlash(__('<span class="label label-success">'.$msg.'<span>'));
		$this->redirect($this->referer());
	}
	
	public function approve($idCmm, $idUser){
		$this->Community->aprovar($idUser, $idCmm);
		$this->Session->setFlash(__('<span class="label label-success">Aprovado com sucesso!<span>'));
		$this->redirect($this->referer());
	}
	
	public function moderator($idCmm, $idUser){
		$this->Community->moderador($idUser, $idCmm);
		$this->Session->setFlash(__('<span class="label label-success">Tornou moderador!<span>'));
		$this->redirect($this->referer());
	}	

	public function ban($idCmm, $idUser){
		$this->Community->banir($idUser, $idCmm);
		$this->Session->setFlash(__('<span class="label label-success">Baniu o usuário!<span>'));
		$this->redirect($this->referer());
	}
	
	public function deleteUser($idCmm, $idUser){
		$this->Community->deletar($idUser, $idCmm);
		$this->Session->setFlash(__('<span class="label label-success">Excluiu o usuário!<span>'));
		$this->redirect($this->referer());
	}
	
	public function joinModerador($idCmm,$url) {
		$idUser = $this->Auth->user('id');
		//insere o usuario como membro da comunidade
		$this->Community->joinModerador($idUser, $idCmm);
		$this->Session->setFlash(__('<span class="label label-success">Você criou a comunidade<span>'));
		$this->redirect('/!'.$url);
	}
	
	public function leave($idCmm) {
		$idUser = $this->Auth->user('id');
		//deleta o usuario como membro da comunidade
		$this->Community->leave($idUser, $idCmm);
		$this->Session->setFlash(__('<span class="label label-success">Você deixou a comunidade<span>'));
		$this->redirect($this->referer());
	}
	
    public function edit($id = null) {
    	$this->layout ='ajax';
        $this->Community->id = $id;
		//lista de tags
		$this-> set ('tags', $this->Community->Tag->tags());	
        if ($this->request->is('get')) {
			$this->request->data = $this->Community->read();
			}else{
				$this->Community->id = $id;
				if ($this->Community->saveAll($this->request->data)) {
					$this->Session->setFlash(__('Sucesso!'));
					$this->redirect(array('action' => 'index'));
				}
			}
	}

    public function delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Community->id = $id;
        if (!$this->Community->exists()) {
            throw new NotFoundException(__('Invalid cmm'));
        }
        if ($this->Community->delete()) {
            $this->Session->setFlash(__('Cmm deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Cmm was not deleted'));
        $this->redirect(array('action' => 'index'));
    }	
	
	public function view($url = null){
		$this->layout = 'communit';
		$idUser = $this-> Auth -> user('id');
		//iD Comunidade
		$id = $this-> Community -> getIdByUrl($url);
		//tags da comunidade
		$this-> set ('tags', $this->Community->Tag->tagsCmm($id));
		$this->Community->id = $id;				
	    if ($this->request->is('get')) {
	        $this->set('community', $this->Community->read());
	        $this->set('topics', $this->Community->Topic->find('all', array('conditions' => array('Topic.community_id' => $id))));
	        $this->set('cmm_url',$url);
	    }
	}
	
	public function members($url = null) {
		$this->layout = 'communit';
		$idUser = $this-> Auth -> user('id');
		$this->set('idUser',$idUser);
		$id = $this-> Community -> getIdByUrl($url);
		$this->set('id',$id);
		$this->set('cmm_url',$url);
		$this-> set ('usersModerador', $this->Community->usersModerador($id));
		$this-> set ('users', $this->Community->users($id));
		$this-> set ('usersPendentes', $this->Community->usersPendentes($id));
	}
	
}